#include "musicslider.h"
#include "ui_musicslider.h"

#include <QMouseEvent>

musicSlider::musicSlider(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::musicSlider),
    maxWidth(1200)
{
    ui->setupUi(this);
}

musicSlider::~musicSlider()
{
    delete ui;
}

void musicSlider::renewSliderPosition(double rate)
{
    currentPos = maxWidth * rate;
    moveSlider();
}

void musicSlider::moveSlider()
{
    ui->outLine->setGeometry(ui->outLine->x(), ui->outLine->y(), currentPos, ui->outLine->height());
}

void musicSlider::mousePressEvent(QMouseEvent *event)
{
    if(event->buttons() == Qt::LeftButton)
    {
        currentPos = event->pos().x();
        currentPos = currentPos < 0 ? 0 : currentPos;
        currentPos = currentPos > maxWidth ? maxWidth : currentPos;
        moveSlider();
    }
}

void musicSlider::mouseReleaseEvent(QMouseEvent *event)
{
    currentPos = event->pos().x();
    currentPos = currentPos < 0 ? 0 : currentPos;
    currentPos = currentPos > maxWidth ? maxWidth : currentPos;
    moveSlider();

    emit setSliderPosition(currentPos * 1.0 / maxWidth);
}

void musicSlider::mouseMoveEvent(QMouseEvent *event)
{
    currentPos = event->pos().x();
    currentPos = currentPos < 0 ? 0 : currentPos;
    currentPos = currentPos > maxWidth ? maxWidth : currentPos;
    moveSlider();
}
